/**
 *
 * @param {*} success 数据库连接成功的回调
 * @param {*} error 数据库连接失败的回调FF
 */
module.exports = function (success, error) {
  //判断 error 为其设置默认值
  if (typeof error !== "function") {
    error = () => {
      console.log("连接失败~~~");
    };
  }
  const mongoose = require("mongoose");
  const { DBHOST, DBPORT, DBNAME } = require("../config/config");
  //设置 strictQuery 为 true
  mongoose.set("strictQuery", true);
  mongoose.connect(`mongodb://${DBHOST}:${DBPORT}/${DBNAME}`);
  mongoose.connection.once("open", () => {
    success();
  });
  // 设置连接错误的回调
  mongoose.connection.on("error", () => {
    error();
  });

  //设置连接关闭的回调
  mongoose.connection.on("close", () => {
    console.log("连接关闭");
  });
};
